/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.gac.masai.getit;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class TitlesFragment extends ListFragment {
	private int mCurPosition = 0;
	//static PrefsFragment pf;



	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// Current position should survive screen rotations.
		if (savedInstanceState != null) {
			mCurPosition = savedInstanceState.getInt("listPosition");
		}

		populateTitles();
		ListView lv = getListView();
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lv.setCacheColorHint(Color.TRANSPARENT);
		selectPosition(mCurPosition);
	}

	public void populateTitles() {
		/*
		 * String[] items = new String[] { "Downloads", "Browser",
		 * "App Manager", "Settings" };
		 */
		String[] items = new String[] { "Downloads", "Browser", "Settings" };
		setListAdapter(new MenuArrayAdapter(getActivity(), items));
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		mCurPosition = position;
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.popBackStack();
        FragmentTransaction transaction = fragmentManager
                        .beginTransaction();
        PrefsFragment pf = new PrefsFragment();
        DownloadFragment df = new DownloadFragment();
        FileBrowserFragment bf = new FileBrowserFragment();

        switch (position) {
        case 0:
                transaction.replace(R.id.frag_content, df);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
        case 1:
                transaction.replace(R.id.frag_content, bf);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
        // Settings
        case 2:
                Log.v("TitlesFragment", "CASE 3");
                transaction.replace(R.id.frag_content, pf);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
        default:
                break;
        }
	}

	public void selectPosition(int position) {
		ListView lv = getListView();
		lv.setItemChecked(position, true);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("listPosition", mCurPosition);
	}
}
